#ifndef HV_BASE_H_
#define HV_BASE_H_
#include <stddef.h>
#ifdef PRINT_DEBUG
#define printd printf
#else
#define printd(...)
#endif
void* safe_malloc(size_t size);
void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
void* safe_calloc(size_t nmemb, size_t size);
void* safe_zalloc(size_t size);
#undef  SAFE_ALLOC
#define SAFE_ALLOC(ptr, size)\
    do {\
        void** pptr = (void**)&(ptr);\
        *pptr = safe_zalloc(size);\
        printd("alloc(%p, size=%lu)\tat [%s:%d:%s]\n", ptr, size, __FILE__, __LINE__, __FUNCTION__);\
    } while(0)

#define SAFE_ALLOC_SIZEOF(ptr)  SAFE_ALLOC(ptr, sizeof(*(ptr)))

#undef  SAFE_FREE
#define SAFE_FREE(ptr)\
    do {\
        if (ptr) {\
            printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
            free(ptr);\
            ptr = NULL;\
        }\
    } while(0)
#endif
